#!/usr/bin/python
# -*- coding: utf-8 -*-

#为方便以后移植，参数中的 key 一律为字符串格式
#
#调用接口为：GetAdminConfig() 和  SetAdminConfig()

import logging

from google.appengine.ext import db
from google.appengine.api import memcache 

import googledatabase


#----------------------------------一下为内部使用函数------------------------
ADMIN_CONFIG_CATCH_NAME = 'Admin_Config'

#从数据库获得管理员配置（系统配置）
def GetAdminConfigFromDatabase():
    Query = db.GqlQuery( 'SELECT * FROM CAdminConfig' )
    AdminConfig = Query.get()

    if AdminConfig == None:
        AdminConfig = googledatabase.CAdminConfig()
        AdminConfig.IntegralPreBlog = 2
        AdminConfig.IntegralPreCommentOn = 1
        AdminConfig.IntegralPreJoin = 10
        
        AdminConfig.MicroblogNeedAuditing = 0
        AdminConfig.AnonymousCanCommentON = 0
        AdminConfig.put()
    
    return AdminConfig

def SetAdminConfigFromDatabase(AdminConfig):
    if AdminConfig == None:
        return
    
    AdminConfig.put()

#-------------------------- 一下为调用接口-------------------------
def GetAdminConfig():
    AdminConfig = memcache.get(ADMIN_CONFIG_CATCH_NAME)
    if AdminConfig:
        return AdminConfig

    AdminConfig = GetAdminConfigFromDatabase()

    memcache.set(ADMIN_CONFIG_CATCH_NAME, AdminConfig)
    return AdminConfig

def SetAdminConfig(AdminConfig):
    if AdminConfig == None:
        return
    
    SetAdminConfigFromDatabase(AdminConfig)
    memcache.set(ADMIN_CONFIG_CATCH_NAME, AdminConfig)

